﻿using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryDal
{
    public class BaseDal<T> where T : BaseEntity
    {
        //数据库上文
        RepositoryDbContext _DbContext;

        public BaseDal(RepositoryDbContext DbContext)
        {
            _DbContext = DbContext;
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            _DbContext.Set<T>().Add(entity);
            int index = _DbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string id)
        {
            T entity = _DbContext.Set<T>().FirstOrDefault(u => u.ID == id);
            if (entity == null)
            {
                return false;
            }
            else
            {
                _DbContext.Set<T>().Remove(entity);
                int index = _DbContext.SaveChanges();
                if (index > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public bool Update(T entity)
        {
            //数据库查出来的实体
            T dbEntity = _DbContext.Set<T>().FirstOrDefault(u => u.ID == entity.ID);

            //反射
            Type type = entity.GetType();
            PropertyInfo[] propertyInfos = type.GetProperties();

            Type type2 = dbEntity.GetType();
            PropertyInfo[] propertyInfos2 = type.GetProperties();

            foreach (PropertyInfo p1 in propertyInfos)
            {
                foreach (PropertyInfo p2 in propertyInfos2)
                {
                    if (p1.Name == p2.Name && p1.GetType() == p2.GetType())
                    {
                        p2.SetValue(dbEntity, p1.GetValue(entity));
                        break;
                    }
                }
            }

            int index = _DbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //public bool Update(T entity)
        //{
        //    //数据库查出来的实体
        //    T dbEntity = _DbContext.Set<T>().FirstOrDefault(u => u.ID == entity.ID);

        //    //反射
        //    foreach (PropertyInfo p1 in entity.GetType().GetProperties())
        //    {
        //        PropertyInfo dbPropertyInfo = dbEntity.GetType().GetProperties().FirstOrDefault(f=> f.Name == p1.Name && f.GetType() == p1.GetType());
        //        if (dbPropertyInfo != null)
        //        {
        //            dbPropertyInfo.SetValue(dbEntity, p1.GetValue(entity));
        //        }
        //    }

        //    int index = _DbContext.SaveChanges();
        //    if (index > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        public bool UpdateRoleInfo(RoleInfo roleInfo)
        {
            //数据库查出来的实体
            RoleInfo entity = _DbContext.RoleInfo.FirstOrDefault(u => u.ID == roleInfo.ID);

            entity.RoleName = roleInfo.RoleName;
            entity.Description = roleInfo.Description;
            int index = _DbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
